// stdafx.cpp : source file that includes just the standard includes
//	TCPIP_TOOL.pch will be the pre-compiled header
//	stdafx.obj will contain the pre-compiled type information

#include "stdafx.h"

CString HexToStr(BYTE *btData, int nLength)
{
	if((btData==NULL)||(nLength<1))	return "";
	CString strCvt,strOutput;   
	int nl = nLength-1;
	for(int i = 0; i < nl; i++)
	{          
		strCvt.Format("%02X ", btData[i]);
		strOutput += strCvt;
	}         
	strCvt.Format("%02X", btData[i]);
	strOutput+=strCvt;
	strCvt.Empty();
	return strOutput;
}

bool IsValidChar(char ch)
{
	char ch_Valid[] = { '0','1','2','3','4','5','6','7','8','9',
		'a','b','c','d','e','f','A','B','C','D','E','F' };
	int iCount = sizeof(ch_Valid) ;
	for( int i=0; i<iCount; i++ )
	{
		if( ch == ch_Valid[i] )
		{
			return true;
		}
	}
	return false;
}

char GetCharFromHex(char hichar,char lochar)
{
	char ret;
	BYTE hi, lo;
	if( hichar>='0' && hichar<='9' )
	{
		hi = hichar-'0';
	}
	else if( hichar>='a' && hichar<='f' )
	{
		hi = hichar-'a'+10;
	}
	else if( hichar>='A' && hichar<='F' )
	{
		hi = hichar-'A'+10;
	}
	else hi = 0 ;
	
	if( lochar>='0' && lochar<='9' )
	{
		lo = lochar-'0';
	}
	else if( lochar>='a' && lochar<='f' )
	{
		lo = lochar-'a'+10;
	}
	else if( lochar>='A' && lochar<='F' )
	{
		lo = lochar-'A'+10;
	}
	else lo = 0 ;
	
	ret = (hi<<4) + lo;

	return ret;
}

CString GetAsciiStr(CString HexStr)
{
	CString AsciiStr;
	AsciiStr.Empty();
	char hichar,lochar;
	int iLen = HexStr.GetLength()/3;
	for( int i=0; i<iLen; i++ )
	{
		hichar = HexStr.GetAt(3*i);
		lochar = HexStr.GetAt(3*i+1);
		char tmpChar = GetCharFromHex( hichar, lochar );
		AsciiStr += tmpChar;
	}
	return AsciiStr;
}

CString GetHexStr(CString AsciiStr)
{
	CString tmpStr;
	CString HexStr;
	HexStr.Empty();
	int iLen = AsciiStr.GetLength();
	for( int i=0; i<iLen; i++ )
	{
		BYTE ch = (BYTE)AsciiStr.GetAt(i);
		tmpStr.Format( "%02X ", ch );
		HexStr += tmpStr ;
	}
	return HexStr;
}

void PackSendBuff( char *buffer, CString strMsg )
{
	char hichar,lochar;
	int iLen = strMsg.GetLength()/3;
	for( int i=0; i<iLen; i++ )
	{
		hichar = strMsg.GetAt(3*i);
		lochar = strMsg.GetAt(3*i+1);
		*(buffer+i) = GetCharFromHex( hichar, lochar );
	}
}


bool QueueNotEmpty(NET_RECV_QUEUE *pQueue)
{
	if( pQueue->front != pQueue->rear )
		return true;
	else
		return false;
}
int Check_QUEUE_Space(NET_RECV_QUEUE *pQueue, unsigned int uiSize, unsigned int MaxLen)
{
	int result = 1;
	unsigned int uiFree;
	if((pQueue->front) >= (pQueue->rear))
	{
		uiFree = MaxLen - (pQueue->front) + (pQueue->rear);
	}
	else
	{
		uiFree = (pQueue->rear) - (pQueue->front);
	}
	if(uiFree <= (uiSize + 1))
	{
		result = 0;
	}
	return result;
}
int InQueue(unsigned char *pData, NET_RECV_QUEUE *pQueue, int nLen)
{
	int i;
	if(!Check_QUEUE_Space(pQueue, nLen, MaxQueue)) return 0;
	for( i=0; i<nLen; i++ )
	{
		pQueue->NetRecvBuff[pQueue->front] = *(pData+i);
		pQueue->front = (pQueue->front+1)%MaxQueue;
	}
	return nLen;
}
int OutQueue(unsigned char *pData, NET_RECV_QUEUE *pQueue, int nLen)
{
	int i;
	if((pQueue->front) == (pQueue->rear)) return 0;
	for(i=0;i<nLen;i++)
	{
		*(pData+i) = pQueue->NetRecvBuff[pQueue->rear];
		pQueue->rear = (pQueue->rear+1)%MaxQueue;
	}
	return nLen;
}
int ReadOneMsg(unsigned char *NetRecvBuff, NET_RECV_QUEUE *pQueue)
{
	int SF = 0, iError= 0;
	int len = 0, nRecv = 0 ;
	unsigned char TempBuff[1];
	while(nRecv<256)
	{
		len = OutQueue(TempBuff, pQueue, 1);
		if(len>0)
		{
			iError = 0 ;
			if((TempBuff[0]==0x68)&&(SF==0))
			{
				SF = 1;
			}
			if( SF==1 )
			{
				memcpy( (NetRecvBuff+nRecv), TempBuff, len) ;
				nRecv += len;
				if ( (NetRecvBuff[0]==0x68) && (nRecv>=NetRecvBuff[1]+2) )
				{
					nRecv = *(NetRecvBuff+1)+2;
					return nRecv;
				}
			}
		}
		else
		{
			iError++;
			if(iError>3) break;
		}
	}
	return 0;
}

